package com.example.langchain4j11chattool.config;

import com.example.langchain4j11chattool.service.FunctionAssistant;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.request.json.JsonObjectSchema;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.tool.ToolExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * @author wwl
 * 日期  2025/9/26 17:36
 * 版本 1.0
 * 描述 :
 */
@Configuration
public class LLMConfig {
    @Bean
    public ChatModel chatModel()
    {
        return OpenAiChatModel.builder()
                .apiKey(System.getenv("aliQwen-api"))
                .modelName("qwen-plus")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .build();
    }
    //低级
    @Bean
    public FunctionAssistant functionAssistant(ChatModel chatModel)
    {
        //工具说明
        ToolSpecification toolSpecification = ToolSpecification.builder()
                .name("开票助手")
                .description("根据用户提交的开票信息，开具发票")
                .parameters(JsonObjectSchema.builder()
                        .addStringProperty("name", "发票抬头")
                        .addStringProperty("address", "发票地址")
                        .addStringProperty("phone", "发票电话")
                        .build())
                .build();
        //业务逻辑
        ToolExecutor toolExecutor = (toolExecutionRequest, memoryId)->{
            System.out.println(toolExecutionRequest.id());
            System.out.println(toolExecutionRequest.name());
            String arg = toolExecutionRequest.arguments();
            System.out.println("arg"+arg);
            return "开张发票成功";
        };
        return AiServices.builder(FunctionAssistant.class)
                .chatModel(chatModel)
                .tools(Map.of(toolSpecification,toolExecutor))
                .build();
    }
}
